﻿using DevTool.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevTool.Contraner
{
    public partial class UserModelControl : UserControl
    {
        public UserModelControl()
        {
            InitializeComponent();
            dblistLoad();
        }
        /// <summary>
        /// 加载数据库
        /// </summary>
        private void dblistLoad()
        {
            var dblist = new GetDB().GetDBName();
            if (dblist != null)
            {
                this.cb_Data.Items.Clear();
                foreach (DataRow item in dblist.Rows)
                {
                    if (item["name"] != null)
                    {
                        this.cb_Data.Items.Add(item["name"].ToString());
                    }

                }
                if (dblist.Rows[0][1] != null)
                {
                    tableload(dblist.Rows[0][1].ToString());
                    this.cb_Data.Text = dblist.Rows[0][1].ToString();
                }

            }

        }
        /// <summary>
        /// 加载表名
        /// </summary>
        /// <param name="dbname"></param>
        private void tableload(string dbname)
        {

            var tablenamelist = new GetDB().GetTableName(dbname);
            this.cb_table.Items.Clear();
            foreach (DataRow item in tablenamelist.Rows)
            {
                if (item["Name"] != null)
                {
                    this.cb_table.Items.Add(item["Name"].ToString());
                }
            }
            if (tablenamelist.Rows[0][1] != null)
            {
                this.cb_table.Text = tablenamelist.Rows[0][1].ToString();
            }
        }
        private void bt_Create_Click(object sender, EventArgs e)
        {
            var dbname = this.cb_Data.Text.Trim();
            var tablename = this.cb_table.Text.Trim();
            var coluns = new GetDB().Getcolnumes(dbname, tablename);
            if (coluns != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("using System;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Data;");
                sb.AppendLine("using System.Linq;");
                sb.AppendLine("using System.Text;");
                sb.AppendLine("namespace " + dbname + " { ");
                sb.AppendLine("public class " + tablename + " {");
                foreach (DataColumn dc in coluns.Columns)
                {
                    if (dc.DataType.Name.Contains("Guid")|| dc.DataType.Name.Contains("DateTime"))
                    {
                        sb.AppendLine("  public " + dc.DataType.Name + "? " + dc.ColumnName + " { get; set; }");
                    }
                    else
                        sb.AppendLine("  public " + dc.DataType.Name + " " + dc.ColumnName + " { get; set; }");
                }

                sb.AppendLine("   }");
                sb.AppendLine(" }");
                this.richTextBox_Comtext.Text = sb.ToString();

            }
        }

        private void cb_Data_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void cb_Data_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dbname = this.cb_Data.Text.Trim();
            tableload(dbname);
        }

        private void bt_resh_Click(object sender, EventArgs e)
        {
            dblistLoad();
            this.richTextBox_Comtext.Clear();
        }

        private void bu_save_Click(object sender, EventArgs e)
        {
            var classname = this.cb_table.Text.Trim();
            this.saveFileDialogModel.Filter = "*.cs|*.txt";
            this.saveFileDialogModel.RestoreDirectory = true;
            this.saveFileDialogModel.FileName = classname + ".cs";
            if (saveFileDialogModel.ShowDialog() == DialogResult.OK)
            {
                var path = saveFileDialogModel.FileName.ToString();
                try
                {
                    using (var fs = new FileStream(path, FileMode.Append))
                    {
                        StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                        sw.Write(this.richTextBox_Comtext.Text);
                        sw.Close();


                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
